package br.trainee.web.componentes;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import br.trainee.web.util.ResponseWrapper;

public class TabTag extends BodyTagSupport {
	
	private String idSubTab;
	private String titulo;
	private String subTabClass;
	private String bodyClass;
	private String delete;
	
	@Override
	public int doEndTag() throws JspException {
		
		String corpoTab = getBodyContent().getString();
		
		TabsTag tabs = (TabsTag) getParent();
		
		JspWriter saida = pageContext.getOut();
		
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
		
		request.setAttribute("idTabs", tabs.getIdTabs());
		request.setAttribute("corpoTab", corpoTab);
		request.setAttribute("idSubTab", idSubTab);
		request.setAttribute("titulo", titulo);
		request.setAttribute("subTabClass", subTabClass);
		request.setAttribute("bodyClass", bodyClass);
		request.setAttribute("delete", delete);
		
		ResponseWrapper responseWrappper = new ResponseWrapper(response);
		
		RequestDispatcher reqDisp = request.getRequestDispatcher("/resources/componentes-jsp/tabs/tab.jsp");
		
		try{
			
			reqDisp.include(request, responseWrappper);
			
			String conteudoComponente = responseWrappper.getString();
			
			saida.println(conteudoComponente);
			
		}
		catch(IOException e){
			e.printStackTrace();
		}
		catch(ServletException e){
			e.printStackTrace();
		}
		
		return super.doEndTag();
	
	}
	
	public String getIdSubTab() {
		return idSubTab;
	}
	public void setIdSubTab(String idSubTab) {
		this.idSubTab = idSubTab;
	}
	public String getTitulo() {
		return titulo;
	}
	public void setTitulo(String titulo) {
		this.titulo = titulo;
	}

	public String getSubTabClass() {
		return subTabClass;
	}

	public void setSubTabClass(String subTabClass) {
		this.subTabClass = subTabClass;
	}

	public String getBodyClass() {
		return bodyClass;
	}

	public void setBodyClass(String bodyClass) {
		this.bodyClass = bodyClass;
	}

	public String getDelete() {
		return delete;
	}

	public void setDelete(String delete) {
		this.delete = delete;
	}

}
